package cn.bocweb.spinning.view.dialog;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;

import java.util.ArrayList;
import java.util.List;

import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.GetQaCtypeBean;
import cn.bocweb.spinning.view.adapter.SelectCommunityAdpater;

/**
 * Created by chenchangxing on 15/12/28.
 */
public class SelectCommunityDialog extends AlertDialog {

    Button finish;
    OnSelectListener mOnSelectListener;
    SelectCommunityAdpater adapater;
    GridView gridView;

    public interface OnSelectListener {
        public void onClick(int position);
    }

    Context context;
    List<GetQaCtypeBean.DataEntity> list;
    public SelectCommunityDialog(Context context) {
        super(context);
        this.context = context;
        list = new ArrayList<>();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_select_community);
        gridView = (GridView)findViewById(R.id.select_community_list);
        finish = (Button)findViewById(R.id.btn_finish);
    }

    @Override
    protected void onStart() {
        super.onStart();
        adapater = new SelectCommunityAdpater(context, list);
        gridView.setAdapter(adapater);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapater.setSelection(position);
                adapater.notifyDataSetChanged();
                mOnSelectListener.onClick(position);
            }
        });

        finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public void setOnSelectListener(OnSelectListener onSelectListener) {
        mOnSelectListener = onSelectListener;
    }

    public void setList(List<GetQaCtypeBean.DataEntity> list) {
        this.list.addAll(list);
    }
}
